/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: UserService
 * Author:   zhouheng
 * Date:     2019/3/29 19:33
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */

package com.onesquare.user.service;

import com.onesquare.common.exception.CommonException;
import com.onesquare.common.exception.ErrorCode;
import com.onesquare.user.client.AuthServiceClient;
import com.onesquare.user.config.RabbitMqConfig;
import com.onesquare.user.dao.UserDao;
import com.onesquare.user.exception.UserLoginException;
import com.onesquare.user.pojo.Jwt;
import com.onesquare.user.pojo.User;
import com.onesquare.user.util.BPwdEncoderUtils;
import com.onesquare.user.util.RedisUtil;
import com.onesquare.common.entity.Result;
import com.util.IdWorker;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/3/29
 * @since 1.0.0
 */
@Service
public class UserService{
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserDao userDao;

    public void sendSms(String mobile) {
        // 1.生成6位随机字符码
        String checkCode = RandomStringUtils.random(6,false,true);
        // 2.将校验码存于redis缓存服务器
        redisUtil.set("checkcode_"+mobile,checkCode);
        // 3.调用短信服务，发送短信
        Map<String,String> map = new HashMap<>();
        map.put("checkCode", checkCode);
        map.put("mobile",mobile);

        rabbitTemplate.convertAndSend(RabbitMqConfig.queueName,map);
        //在控制台显示一份【方便测试】
        System.out.println("验证码为："+checkCode);

    }

    /**
     * @description:
     * @date: 2019/3/30 20:25
     * @author:zhouheng
     */
    public void add(User user) {
        user.setId(idWorker.nextId()+"" );
        //密码加密
        user.setPassword(BPwdEncoderUtils.BCryptPassword(user.getPassword()));
        user.setFollowcount(0);//关注数
        user.setFanscount(0);//粉丝数
        user.setOnline(0L);//在线时长
        user.setRegdate(new Date());//注册日期
        user.setUpdatedate(new Date());//更新日期
        user.setLastdate(new Date());//最后登陆日期

        userDao.save(user);
    }

    /**
     * @description: 
     * @date: 2019/3/31 20:23
     * @author:zhouheng
     */
    public User login(String mobile, String password) {
        User user = userDao.findByMobile(mobile);
        if(null == user){
            throw new CommonException(ErrorCode.USER_NOT_FOUND);
        }

        if(!BPwdEncoderUtils.matches(password, user.getPassword())){
            throw new CommonException(ErrorCode.USER_PASSWORD_ERROR);
        }

        return user;

    }
}
